import { Verify } from 'src/app/service/verify.service';
import { Console } from 'console';
import { Message } from '@angular/compiler/src/i18n/i18n_ast';
import { PostponeApply } from './../../domains/postponeApply.domain';
import { ActivatedRoute, Data } from '@angular/router';
import { PayRecord } from './../../domains/PayRecord.domain';
import { Component, OnInit } from '@angular/core';
import { PayRecordServiceSvr } from 'src/app/service/payRecord.service';
import { NzTableFilterFn, NzTableFilterList, NzTableSortFn, NzTableSortOrder } from 'ng-zorro-antd/table';
import { PayRecordList } from 'src/app/domains/payrecordlist.domain';
import { PostponeApplyServiceSvr } from 'src/app/service/postponeApply.service';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-rent-management-payment',
  templateUrl: './rent-management-payment.component.html',
  styleUrls: ['./rent-management-payment.component.less']
})


export class RentManagementPaymentComponent implements OnInit {

  constructor(public payRecord:PayRecordServiceSvr,
    private activatedRoute: ActivatedRoute,
    private postponeApplyServiceSvr: PostponeApplyServiceSvr,
    private message: NzMessageService,
    private verify: Verify
  ) {}

  token: string = JSON.parse(localStorage.getItem('Token')!)
  customerId: string = JSON.parse(localStorage.getItem('customerId')!)
  res:PayRecordList = new PayRecordList()
  payment = new PayRecord()
  payList: any
  houseId: string =''
  isPayList: any
  size: number = 8
  page: number = 1
  isOrNotPayList: any
  delayModel: boolean = false
  delayDay: number = 0
  delayReason: string =''
  contractId: string = ''


  ngOnInit(): void {

    // this.customerId = JSON.parse(localStorage.getItem('customerId')!)
    this.getCurrentHouseId()
    this.getPayList()
  }

  // 获取全部缴费记录（未缴+已缴）
  async getPayList() {
    try {
      this.res = await this.payRecord.listPaymentRecord('', this.payment.Status,
      this.customerId, this.payment.CustomerName, this.houseId, '', '',
      this.payment.IsPostpone, this.payment.Type, this.page, this.size, 0)
    } catch (error) {
      console.log(error)
    }
    this.payList = this.res._Items
    this.isOrNotPayList = this.payList
    console.log('this: ', this.isOrNotPayList)
  }

  // 获取当前的房屋Id
  getCurrentHouseId() {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      this.houseId = JSON.parse(queryParams['houseData']).Id
    })
  }

  // 判断缴费类型
  getPayType(type: number) {
    switch(type) {
      case 1: return '租金';
      case 2: return '押金';
      case 3: return '修理费';
      case 4: return '定金';
    }
    if(type === 1) return '租金'
    else if(type === 2) return '押金'
    else if(type === 3) return '修理费'
    else if(type === 4) return '定金'
    else return '其它'
  }

  // 格式化时间
  formatTime(time: string | number | Date) {
    time = new Date(time)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let date = time.getDate()
    return year+'-'+month+'-'+date
  }

  // 获取已缴费列表
  getIsPayList() {
    this.isOrNotPayList = this.payList.filter((ele: { Status: number; }) => {
      if(ele.Status === 1) return ele
      return;
    })
  }

  // 获取未缴费列表
  getNotPayList() {
    this.isOrNotPayList = this.payList.filter((ele: { Status: number; }) => {
      if(ele.Status === -1) return ele
      return;
    })
  }

  // 申请延期
  async applyDelay(id: string) {
    this.contractId = id
    this.delayModel = true;
  }

  // 取消申请弹窗
  handleCancelDelayApply(): void {
    this.message.info('取消延期申请')
    this.delayModel = false;
  }

  // 确定延期
  async handleOkDelay(): Promise<void> {
    if(this.verify.isNotNull(this.delayReason)&&this.delayDay!==0) {
      this.delayModel = false
      let result
      try {
        result = await this.postponeApplyServiceSvr.savePostponeApply(this.token, this.contractId, this.delayReason, this.delayDay.toString())
        this.getPayList()
        this.message.success('延期申请提交成功')
      } catch (error) {
        this.message.error('延期申请提交失败')
        console.log(error)
      }
    } else {
      if(this.delayDay===0) {
        this.message.error('延期天数不能为0')
      }
    }
  }

  // 缴费
  async pay(data: PayRecord) {
    let res = await this.payRecord.pay(this.token, data.Id, data.PayablePrice, '', data.PayWay)
    console.log(res)
    document.write(res)
  }

  close() {
    this.delayReason = ''
    this.delayDay = 0
  }


}
